import pygame
import random
import sys
import math
from enum import Enum

# 初始化pygame
pygame.init()

# 游戏设置
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
PURPLE = (128, 0, 128)
CYAN = (0, 255, 255)
GRAY = (128, 128, 128)
LIGHT_GRAY = (200, 200, 200)
DARK_GRAY = (64, 64, 64)

# 游戏元素尺寸
PADDLE_WIDTH = 100
PADDLE_HEIGHT = 15
BALL_SIZE = 10
BRICK_WIDTH = 75
BRICK_HEIGHT = 20
BRICK_PADDING = 5
BRICK_OFFSET_TOP = 60
BRICK_OFFSET_LEFT = 35
BRICK_ROWS = 5
BRICK_COLS = 9

# 游戏状态
class GameState(Enum):
    MENU = 1
    PLAYING = 2
    PAUSED = 3
    GAME_OVER = 4
    WON = 5

# 砖块类型
class BrickType(Enum):
    NORMAL = 1      # 普通砖块 - 1次击破
    HARD = 2        # 硬砖块 - 2次击破
    METAL = 3       # 金属砖块 - 3次击破
    EXPLOSIVE = 4   # 爆炸砖块 - 击破时爆炸周围砖块
    BONUS = 5       # 奖励砖块 - 击破获得道具

# 道具类型
class PowerUpType(Enum):
    EXPAND_PADDLE = 1  # 加长挡板
    MULTI_BALL = 2     # 多球
    SLOW_BALL = 3      # 减速球
    LASER = 4          # 激光
    EXTRA_LIFE = 5     # 额外生命

class Brick:
    def __init__(self, x, y, brick_type=BrickType.NORMAL):
        self.x = x
        self.y = y
        self.width = BRICK_WIDTH
        self.height = BRICK_HEIGHT
        self.type = brick_type
        self.hits_required = self.get_hits_required()
        self.rect = pygame.Rect(x, y, self.width, self.height)
        self.visible = True

    def get_hits_required(self):
        if self.type == BrickType.NORMAL:
            return 1
        elif self.type == BrickType.HARD:
            return 2
        elif self.type == BrickType.METAL:
            return 3
        elif self.type == BrickType.EXPLOSIVE:
            return 1
        elif self.type == BrickType.BONUS:
            return 1
        return 1

    def get_color(self):
        if self.type == BrickType.NORMAL:
            return RED
        elif self.type == BrickType.HARD:
            return ORANGE
        elif self.type == BrickType.METAL:
            return GRAY
        elif self.type == BrickType.EXPLOSIVE:
            return YELLOW
        elif self.type == BrickType.BONUS:
            return GREEN
        return WHITE

    def hit(self):
        self.hits_required -= 1
        if self.hits_required <= 0:
            self.visible = False
            return True
        return False

    def draw(self, screen):
        if self.visible:
            color = self.get_color()
            pygame.draw.rect(screen, color, self.rect)

            # 绘制砖块边框
            pygame.draw.rect(screen, BLACK, self.rect, 2)

            # 为不同类型的砖块添加装饰
            if self.type == BrickType.METAL:
                # 金属砖块添加条纹
                for i in range(0, self.width, 10):
                    pygame.draw.line(screen, DARK_GRAY,
                                   (self.rect.x + i, self.rect.y),
                                   (self.rect.x + i + 5, self.rect.y + self.height), 2)
            elif self.type == BrickType.HARD:
                # 硬砖块添加裂纹
                center_x = self.rect.centerx
                center_y = self.rect.centery
                pygame.draw.line(screen, BLACK,
                               (center_x - 5, center_y - 5),
                               (center_x + 5, center_y + 5), 1)
                pygame.draw.line(screen, BLACK,
                               (center_x - 5, center_y + 5),
                               (center_x + 5, center_y - 5), 1)
            elif self.type == BrickType.EXPLOSIVE:
                # 爆炸砖块添加警告符号
                pygame.draw.circle(screen, RED, self.rect.center, 8, 2)
                font = pygame.font.Font(None, 16)
                text = font.render("!", True, RED)
                text_rect = text.get_rect(center=self.rect.center)
                screen.blit(text, text_rect)
            elif self.type == BrickType.BONUS:
                # 奖励砖块添加星星
                center = self.rect.center
                points = []
                for i in range(10):
                    angle = math.pi * i / 5
                    if i % 2 == 0:
                        radius = 8
                    else:
                        radius = 4
                    x = center[0] + radius * math.cos(angle - math.pi / 2)
                    y = center[1] + radius * math.sin(angle - math.pi / 2)
                    points.append((x, y))
                pygame.draw.polygon(screen, YELLOW, points)

class Ball:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.vx = random.choice([-4, 4])
        self.vy = -4
        self.radius = BALL_SIZE
        self.speed = 4
        self.rect = pygame.Rect(x - self.radius, y - self.radius,
                              self.radius * 2, self.radius * 2)
        self.trail = []  # 球的轨迹
        self.max_trail_length = 10

    def update(self):
        # 更新轨迹
        self.trail.append((self.x, self.y))
        if len(self.trail) > self.max_trail_length:
            self.trail.pop(0)

        self.x += self.vx
        self.y += self.vy
        self.rect.x = self.x - self.radius
        self.rect.y = self.y - self.radius

    def bounce_x(self):
        self.vx = -self.vx

    def bounce_y(self):
        self.vy = -self.vy

    def set_speed(self, new_speed):
        # 保持方向，只改变速度大小
        current_speed = math.sqrt(self.vx**2 + self.vy**2)
        if current_speed > 0:
            ratio = new_speed / current_speed
            self.vx *= ratio
            self.vy *= ratio
        self.speed = new_speed

    def draw(self, screen):
        # 绘制轨迹
        for i, pos in enumerate(self.trail):
            alpha = int(255 * (i / len(self.trail)) * 0.3)
            color = (*CYAN, alpha)
            pygame.draw.circle(screen, CYAN, (int(pos[0]), int(pos[1])),
                             self.radius - 2, 1)

        # 绘制球体
        pygame.draw.circle(screen, WHITE, (int(self.x), int(self.y)), self.radius)
        pygame.draw.circle(screen, CYAN, (int(self.x), int(self.y)), self.radius, 2)

class Paddle:
    def __init__(self):
        self.width = PADDLE_WIDTH
        self.height = PADDLE_HEIGHT
        self.x = SCREEN_WIDTH // 2 - self.width // 2
        self.y = SCREEN_HEIGHT - 50
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
        self.speed = 8
        self.color = BLUE

    def move_left(self):
        self.x = max(0, self.x - self.speed)
        self.rect.x = self.x

    def move_right(self):
        self.x = min(SCREEN_WIDTH - self.width, self.x + self.speed)
        self.rect.x = self.x

    def expand(self):
        self.width = min(200, self.width + 30)
        self.rect.width = self.width

    def draw(self, screen):
        pygame.draw.rect(screen, self.color, self.rect)
        pygame.draw.rect(screen, WHITE, self.rect, 2)

        # 添加装饰条纹
        for i in range(0, self.width, 20):
            pygame.draw.line(screen, WHITE,
                           (self.rect.x + i, self.rect.y),
                           (self.rect.x + i + 10, self.rect.y + self.height), 1)

class PowerUp:
    def __init__(self, x, y, power_type):
        self.x = x
        self.y = y
        self.width = 30
        self.height = 30
        self.type = power_type
        self.speed = 2
        self.rect = pygame.Rect(x, y, self.width, self.height)
        self.animation_timer = 0

    def update(self):
        self.y += self.speed
        self.rect.y = self.y
        self.animation_timer += 1

    def get_color(self):
        if self.type == PowerUpType.EXPAND_PADDLE:
            return BLUE
        elif self.type == PowerUpType.MULTI_BALL:
            return GREEN
        elif self.type == PowerUpType.SLOW_BALL:
            return YELLOW
        elif self.type == PowerUpType.LASER:
            return RED
        elif self.type == PowerUpType.EXTRA_LIFE:
            return CYAN
        return WHITE

    def draw(self, screen):
        color = self.get_color()
        center = self.rect.center

        # 动画效果
        bounce = abs(math.sin(self.animation_timer * 0.1)) * 3

        if self.type == PowerUpType.EXPAND_PADDLE:
            # 绘制加长挡板图标
            pygame.draw.rect(screen, color,
                           (center[0] - 15, center[1] - 2 + bounce, 30, 4))
        elif self.type == PowerUpType.MULTI_BALL:
            # 绘制多球图标
            pygame.draw.circle(screen, color,
                             (center[0] - 5, center[1] + int(bounce)), 5)
            pygame.draw.circle(screen, color,
                             (center[0] + 5, center[1] - int(bounce)), 5)
        elif self.type == PowerUpType.SLOW_BALL:
            # 绘制减速图标
            for i in range(3):
                radius = 10 - i * 3
                alpha = 255 - i * 80
                pygame.draw.circle(screen, color, center, radius, 2)
        elif self.type == PowerUpType.LASER:
            # 绘制激光图标
            pygame.draw.rect(screen, color,
                           (center[0] - 2, center[1] - 10 + bounce, 4, 20))
        elif self.type == PowerUpType.EXTRA_LIFE:
            # 绘制心形
            points = []
            for angle in range(0, 360, 30):
                rad = math.radians(angle)
                if angle < 180:
                    x = center[0] + 8 * math.cos(rad) * 1.2
                    y = center[1] + 8 * math.sin(rad) * 1.2 - 3
                else:
                    x = center[0] + 8 * math.cos(rad)
                    y = center[1] + 8 * math.sin(rad) - 3
                points.append((x, y))
            pygame.draw.polygon(screen, color, points)

class Particle:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.vx = random.uniform(-5, 5)
        self.vy = random.uniform(-5, 5)
        self.color = color
        self.life = 30
        self.size = random.randint(2, 6)

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += 0.2  # 重力
        self.life -= 1
        self.size = max(1, self.size - 0.1)

    def draw(self, screen):
        if self.life > 0:
            alpha = int(255 * (self.life / 30))
            pygame.draw.circle(screen, self.color,
                             (int(self.x), int(self.y)),
                             int(self.size))

class Game:
    def __init__(self):
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        pygame.display.set_caption("打砖块 - Breakout Game")
        self.clock = pygame.time.Clock()

        # 字体
        self.font_large = pygame.font.Font(None, 48)
        self.font_medium = pygame.font.Font(None, 36)
        self.font_small = pygame.font.Font(None, 24)

        # 游戏状态
        self.state = GameState.MENU

        # 游戏对象
        self.paddle = Paddle()
        self.balls = []
        self.bricks = []
        self.powerups = []
        self.particles = []

        # 游戏统计
        self.score = 0
        self.lives = 3
        self.level = 1
        self.combo = 0
        self.max_combo = 0

    def create_level(self, level_num):
        """创建关卡"""
        self.bricks.clear()

        # 根据关卡生成不同难度的砖块布局
        for row in range(BRICK_ROWS):
            for col in range(BRICK_COLS):
                x = BRICK_OFFSET_LEFT + col * (BRICK_WIDTH + BRICK_PADDING)
                y = BRICK_OFFSET_TOP + row * (BRICK_HEIGHT + BRICK_PADDING)

                # 根据关卡和位置确定砖块类型
                brick_type = self.get_brick_type(level_num, row, col)
                brick = Brick(x, y, brick_type)
                self.bricks.append(brick)

    def get_brick_type(self, level, row, col):
        """根据关卡和位置确定砖块类型"""
        rand = random.random()

        # 随着关卡增加，出现更多特殊砖块
        if level == 1:
            # 第一关主要是普通砖块
            if rand < 0.1:
                return BrickType.HARD
            return BrickType.NORMAL
        elif level == 2:
            if rand < 0.05:
                return BrickType.BONUS
            elif rand < 0.15:
                return BrickType.HARD
            elif rand < 0.2:
                return BrickType.EXPLOSIVE
            return BrickType.NORMAL
        else:
            # 第三关及以后
            if rand < 0.05:
                return BrickType.METAL
            elif rand < 0.1:
                return BrickType.BONUS
            elif rand < 0.2:
                return BrickType.HARD
            elif rand < 0.3:
                return BrickType.EXPLOSIVE
            return BrickType.NORMAL

    def reset_ball(self):
        """重置球的位置"""
        self.balls.clear()
        ball = Ball(self.paddle.x + self.paddle.width // 2,
                   self.paddle.y - BALL_SIZE * 2)
        self.balls.append(ball)

    def create_particles(self, x, y, color, count=10):
        """创建粒子效果"""
        for _ in range(count):
            self.particles.append(Particle(x, y, color))

    def handle_collision(self, ball, brick):
        """处理球与砖块的碰撞"""
        if not brick.visible:
            return False

        if ball.rect.colliderect(brick.rect):
            # 判断碰撞方向
            ball_center_x = ball.x
            ball_center_y = ball.y
            brick_center_x = brick.rect.centerx
            brick_center_y = brick.rect.centery

            dx = abs(ball_center_x - brick_center_x)
            dy = abs(ball_center_y - brick_center_y)

            if dx > dy:
                ball.bounce_x()
            else:
                ball.bounce_y()

            # 击中砖块
            if brick.hit():
                self.score += self.get_brick_score(brick.type)
                self.combo += 1
                self.max_combo = max(self.max_combo, self.combo)

                # 创建粒子效果
                self.create_particles(brick.rect.centerx, brick.rect.centery,
                                   brick.get_color(), 15)

                # 特殊砖块效果
                if brick.type == BrickType.EXPLOSIVE:
                    self.explode_nearby_bricks(brick)
                elif brick.type == BrickType.BONUS:
                    self.drop_powerup(brick.rect.centerx, brick.rect.centery)

            return True
        return False

    def explode_nearby_bricks(self, explosive_brick):
        """爆炸砖块效果"""
        explosion_radius = 100
        center_x = explosive_brick.rect.centerx
        center_y = explosive_brick.rect.centery

        for brick in self.bricks[:]:
            if brick.visible:
                brick_center_x = brick.rect.centerx
                brick_center_y = brick.rect.centery

                distance = math.sqrt((brick_center_x - center_x)**2 +
                                   (brick_center_y - center_y)**2)

                if distance <= explosion_radius:
                    if brick.hit():
                        self.score += self.get_brick_score(brick.type)
                        self.create_particles(brick.rect.centerx, brick.rect.centery,
                                           brick.get_color(), 10)

    def drop_powerup(self, x, y):
        """掉落道具"""
        power_type = random.choice(list(PowerUpType))
        powerup = PowerUp(x - 15, y, power_type)
        self.powerups.append(powerup)

    def get_brick_score(self, brick_type):
        """获取砖块分数"""
        base_scores = {
            BrickType.NORMAL: 10,
            BrickType.HARD: 20,
            BrickType.METAL: 30,
            BrickType.EXPLOSIVE: 40,
            BrickType.BONUS: 50
        }
        score = base_scores.get(brick_type, 10)

        # 连击奖励
        combo_bonus = self.combo * 5
        return score + combo_bonus

    def apply_powerup(self, powerup):
        """应用道具效果"""
        if powerup.type == PowerUpType.EXPAND_PADDLE:
            self.paddle.expand()
        elif powerup.type == PowerUpType.MULTI_BALL:
            current_ball = self.balls[0] if self.balls else None
            if current_ball:
                for _ in range(2):
                    new_ball = Ball(current_ball.x, current_ball.y)
                    new_ball.vx = random.choice([-4, 4])
                    new_ball.vy = random.choice([-4, -3])
                    self.balls.append(new_ball)
        elif powerup.type == PowerUpType.SLOW_BALL:
            for ball in self.balls:
                ball.set_speed(max(2, ball.speed - 1))
        elif powerup.type == PowerUpType.EXTRA_LIFE:
            self.lives += 1

        # 创建特效
        self.create_particles(powerup.rect.centerx, powerup.rect.centery,
                           powerup.get_color(), 20)

    def update(self):
        """更新游戏状态"""
        if self.state != GameState.PLAYING:
            return

        # 更新球
        for ball in self.balls[:]:
            ball.update()

            # 墙壁碰撞
            if ball.x <= ball.radius or ball.x >= SCREEN_WIDTH - ball.radius:
                ball.bounce_x()
            if ball.y <= ball.radius:
                ball.bounce_y()

            # 底部碰撞 - 失去生命
            if ball.y >= SCREEN_HEIGHT:
                self.balls.remove(ball)
                self.combo = 0  # 重置连击

                if not self.balls:
                    self.lives -= 1
                    if self.lives <= 0:
                        self.state = GameState.GAME_OVER
                    else:
                        self.reset_ball()

            # 挡板碰撞
            if ball.rect.colliderect(self.paddle.rect):
                ball.bounce_y()

                # 根据击球位置调整球的水平速度
                hit_pos = (ball.x - self.paddle.x) / self.paddle.width
                ball.vx = 8 * (hit_pos - 0.5)

            # 砖块碰撞
            for brick in self.bricks:
                self.handle_collision(ball, brick)

        # 更新道具
        for powerup in self.powerups[:]:
            powerup.update()

            # 挡板接住道具
            if powerup.rect.colliderect(self.paddle.rect):
                self.apply_powerup(powerup)
                self.powerups.remove(powerup)
            elif powerup.y > SCREEN_HEIGHT:
                self.powerups.remove(powerup)

        # 更新粒子
        for particle in self.particles[:]:
            particle.update()
            if particle.life <= 0:
                self.particles.remove(particle)

        # 检查胜利条件
        if all(not brick.visible for brick in self.bricks):
            self.state = GameState.WON

    def draw_menu(self):
        """绘制主菜单"""
        self.screen.fill(BLACK)

        # 标题
        title = self.font_large.render("BREAKOUT", True, WHITE)
        title_rect = title.get_rect(center=(SCREEN_WIDTH // 2, 150))
        self.screen.blit(title, title_rect)

        # 说明
        instructions = [
            "Use LEFT/RIGHT ARROW KEYS to move paddle",
            "Press SPACE to start ball",
            "Break all bricks to win!",
            "",
            "Brick Types:",
            "RED: Normal (10 pts)",
            "ORANGE: Hard (20 pts)",
            "GRAY: Metal (30 pts)",
            "YELLOW: Explosive (40 pts)",
            "GREEN: Bonus (50 pts)",
            "",
            "Press SPACE to Start",
            "Press ESC to Quit"
        ]

        y = 220
        for instruction in instructions:
            color = WHITE
            if "RED:" in instruction:
                color = RED
            elif "ORANGE:" in instruction:
                color = ORANGE
            elif "GRAY:" in instruction:
                color = GRAY
            elif "YELLOW:" in instruction:
                color = YELLOW
            elif "GREEN:" in instruction:
                color = GREEN

            text = self.font_small.render(instruction, True, color)
            text_rect = text.get_rect(center=(SCREEN_WIDTH // 2, y))
            self.screen.blit(text, text_rect)
            y += 25

    def draw_game(self):
        """绘制游戏画面"""
        self.screen.fill(BLACK)

        # 绘制砖块
        for brick in self.bricks:
            brick.draw(self.screen)

        # 绘制挡板
        self.paddle.draw(self.screen)

        # 绘制球
        for ball in self.balls:
            ball.draw(self.screen)

        # 绘制道具
        for powerup in self.powerups:
            powerup.draw(self.screen)

        # 绘制粒子
        for particle in self.particles:
            particle.draw(self.screen)

        # 绘制UI
        self.draw_ui()

    def draw_ui(self):
        """绘制用户界面"""
        # 分数
        score_text = self.font_medium.render(f"Score: {self.score}", True, WHITE)
        self.screen.blit(score_text, (10, 10))

        # 生命值
        lives_text = self.font_medium.render(f"Lives: {self.lives}", True, WHITE)
        self.screen.blit(lives_text, (10, 50))

        # 关卡
        level_text = self.font_medium.render(f"Level: {self.level}", True, WHITE)
        self.screen.blit(level_text, (10, 90))

        # 连击
        if self.combo > 1:
            combo_text = self.font_small.render(f"Combo x{self.combo}", True, YELLOW)
            self.screen.blit(combo_text, (10, 130))

        # 操作提示
        hint_text = self.font_small.render("ESC - Pause", True, GRAY)
        self.screen.blit(hint_text, (SCREEN_WIDTH - 120, 10))

    def draw_paused(self):
        """绘制暂停画面"""
        self.draw_game()

        # 半透明遮罩
        overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
        overlay.set_alpha(128)
        overlay.fill(BLACK)
        self.screen.blit(overlay, (0, 0))

        # 暂停文字
        paused_text = self.font_large.render("PAUSED", True, WHITE)
        paused_rect = paused_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
        self.screen.blit(paused_text, paused_rect)

        resume_text = self.font_medium.render("Press ESC to Resume", True, WHITE)
        resume_rect = resume_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 50))
        self.screen.blit(resume_text, resume_rect)

    def draw_game_over(self):
        """绘制游戏结束画面"""
        self.draw_game()

        # 半透明遮罩
        overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
        overlay.set_alpha(128)
        overlay.fill(BLACK)
        self.screen.blit(overlay, (0, 0))

        # 游戏结束文字
        game_over_text = self.font_large.render("GAME OVER", True, RED)
        game_over_rect = game_over_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 - 50))
        self.screen.blit(game_over_text, game_over_rect)

        # 统计信息
        score_text = self.font_medium.render(f"Final Score: {self.score}", True, WHITE)
        score_rect = score_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
        self.screen.blit(score_text, score_rect)

        max_combo_text = self.font_medium.render(f"Max Combo: {self.max_combo}", True, YELLOW)
        combo_rect = max_combo_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 40))
        self.screen.blit(max_combo_text, combo_rect)

        # 重新开始提示
        restart_text = self.font_medium.render("Press SPACE to Try Again", True, GREEN)
        restart_rect = restart_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 90))
        self.screen.blit(restart_text, restart_rect)

        menu_text = self.font_medium.render("Press ESC for Menu", True, WHITE)
        menu_rect = menu_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 130))
        self.screen.blit(menu_text, menu_rect)

    def draw_won(self):
        """绘制胜利画面"""
        self.draw_game()

        # 半透明遮罩
        overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
        overlay.set_alpha(128)
        overlay.fill(BLACK)
        self.screen.blit(overlay, (0, 0))

        # 胜利文字
        won_text = self.font_large.render("LEVEL COMPLETE!", True, GREEN)
        won_rect = won_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 - 50))
        self.screen.blit(won_text, won_rect)

        # 统计信息
        score_text = self.font_medium.render(f"Score: {self.score}", True, WHITE)
        score_rect = score_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
        self.screen.blit(score_text, score_rect)

        max_combo_text = self.font_medium.render(f"Max Combo: {self.max_combo}", True, YELLOW)
        combo_rect = max_combo_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 40))
        self.screen.blit(max_combo_text, combo_rect)

        # 继续提示
        continue_text = self.font_medium.render("Press SPACE for Next Level", True, GREEN)
        continue_rect = continue_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 90))
        self.screen.blit(continue_text, continue_rect)

    def draw(self):
        """绘制游戏"""
        if self.state == GameState.MENU:
            self.draw_menu()
        elif self.state == GameState.PLAYING:
            self.draw_game()
        elif self.state == GameState.PAUSED:
            self.draw_paused()
        elif self.state == GameState.GAME_OVER:
            self.draw_game_over()
        elif self.state == GameState.WON:
            self.draw_won()

        pygame.display.flip()

    def handle_events(self):
        """处理事件"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    if self.state == GameState.PLAYING:
                        self.state = GameState.PAUSED
                    elif self.state == GameState.PAUSED:
                        self.state = GameState.PLAYING
                    else:
                        return False

                if self.state == GameState.MENU:
                    if event.key == pygame.K_SPACE:
                        self.state = GameState.PLAYING
                        self.reset_game()

                elif self.state == GameState.PLAYING:
                    if event.key == pygame.K_SPACE and len(self.balls) == 1:
                        # 发射球
                        if self.balls[0].y >= self.paddle.y - BALL_SIZE * 2 - 5:
                            self.balls[0].vy = -abs(self.balls[0].vy)

                elif self.state == GameState.GAME_OVER:
                    if event.key == pygame.K_SPACE:
                        self.state = GameState.PLAYING
                        self.reset_game()

                elif self.state == GameState.WON:
                    if event.key == pygame.K_SPACE:
                        self.level += 1
                        self.state = GameState.PLAYING
                        self.create_level(self.level)
                        self.reset_ball()

        # 持续按键检测
        if self.state == GameState.PLAYING:
            keys = pygame.key.get_pressed()
            if keys[pygame.K_LEFT]:
                self.paddle.move_left()
            if keys[pygame.K_RIGHT]:
                self.paddle.move_right()

        return True

    def reset_game(self):
        """重置游戏"""
        self.score = 0
        self.lives = 3
        self.level = 1
        self.combo = 0
        self.max_combo = 0
        self.particles.clear()
        self.powerups.clear()

        self.create_level(self.level)
        self.reset_ball()

    def run(self):
        """运行游戏"""
        running = True

        while running:
            running = self.handle_events()
            self.update()
            self.draw()
            self.clock.tick(FPS)

        pygame.quit()
        sys.exit()

# 运行游戏
if __name__ == "__main__":
    game = Game()
    game.run()